package UIComponents// ActionScript file
{
	import MyComponent.MyButton;
	
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	public class UIMission extends Sprite
	{
		private var _mission:IMission;
		public var missFinish:MyButton;
		private var missSure:MyButton;
		
		public function UIMission():void
		{
			this.init();
		}
		
		private function init():void
		{
			this.missFinish=this.getChildByName("button_finish") as MyButton;
			this.missSure=this.getChildByName("missBtnSure") as MyButton;
			this.getChildByName("fishtxt").visible=this.getChildByName("sureTxt").visible=false;
			(this.getChildByName("fishtxt") as TextField).mouseEnabled=(this.getChildByName("sureTxt") as TextField).mouseEnabled=false;
			this.missFinish.visible=false;
			this.missSure.visible=false;
			this.addListener();
		}
		
		private function addListener():void
		{
			 this.missFinish.addEventListener(ComponentEvent.BUTTON_DOWN,finishHandler);
			 this.missSure.addEventListener(ComponentEvent.BUTTON_DOWN,closeHandler);
			(this.getChildByName("button_close") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,closeHandler);
		}
		
		private function finishHandler(e:ComponentEvent):void
		{
			if(this.mission)this.mission.finishHandler();
		}
		
		private function closeHandler(e:Event):void
		{
			this.dispatchEvent(new PanelEvent(PanelEvent.CLOSE,true));
		}
		
		public function setMission(mission:IMission):void
		{
			this._mission=mission;
			this.reshow();
		}
		
		private function reset():void
		{
			(this.getChildByName("txt_missionName") as TextField).text="";
			(this.getChildByName("txt_missionDescription") as TextField).text="";
//			(this.getChildByName("txt_tip") as TextField).text="";
			this.isFinishMissFlag=true;
		}
		private var isFinishMissFlag:Boolean=true;
		private function reshow():void
		{
			this.reset();
			(this.getChildByName("txt_missionName") as TextField).text=this.mission.missionName;
			(this.getChildByName("txt_missionDescription") as TextField).text=this.mission.missionDescription;
//			(this.getChildByName("txt_tip") as TextField).text=this.mission.missionTip;
			var dis:DisplayObject=this.mission.npcIcon;
			if(dis!=null)
			{
				dis.scaleX=0.9;
				dis.scaleY=0.9;
			}
			(this.getChildByName("_iconContainer") as IconContainer).addPic(dis);
			var conditionCountsAry:Array=this.mission.conditionCountsAry;
			var rewardCountsAry:Array=this.mission.rewardCountsAry;
			var i:int;
			for(i=0;i<3;i++)
			{
				var conditionCountObj:Object=conditionCountsAry[i];
				var dt:TextField=this.getChildByName("txt_count"+(i+1)+"Description") as TextField;
				var vt:TextField=this.getChildByName("txt_count"+(i+1)+"Value") as TextField;
				if(conditionCountObj)
				{
					dt.text=conditionCountObj.description;
					vt.text=conditionCountObj.nowValue+"/"+conditionCountObj.targetValue;
					if(int(conditionCountObj.nowValue)<int(conditionCountObj.targetValue))
					{
						this.isFinishMissFlag=false;
					}
				}
				else
				{
					dt.text="";
					vt.text="";
				}
			}
			for(i=0;i<6;i++)
			{
				var frameMissionReward:IconContainer=this.getChildByName("frameReward"+(i+1)) as IconContainer;
				var rewardPic:DisplayObject=rewardCountsAry[i];
				if(rewardPic)
				{
					frameMissionReward.visible=true;
					frameMissionReward.addPic(rewardPic,rewardPic["displayViewIconName"]);
					//frameMissionReward.setRewardValue(rewardCountObj.targetValue);
				}
				else
				{
					frameMissionReward.visible=false;
				}
			}
			
			this.controlBtn(isFinishMissFlag);
		}
		
		private function controlBtn(isFlage:Boolean):void
		{
			this.missFinish.visible=isFlage;
			this.missSure.visible=!isFlage;
			this.getChildByName("fishtxt").visible=isFlage;
			this.getChildByName("sureTxt").visible=!isFlage;
		}

		/*getter & setter*/
		public function get mission():IMission
		{
			return _mission;
		}

	}
}